package br.usp.poli.ws;

import java.util.List;
import java.util.Set;

import org.hibernate.Session;

import br.usp.poli.dao.ExperimentDao;
import br.usp.poli.dao.GenericDao;
import br.usp.poli.dao.VariableDao;
import br.usp.poli.entity.Experiment;
import br.usp.poli.entity.Occurrence;
import br.usp.poli.entity.Variable;
import br.usp.poli.util.db.HibernateUtil;

public class ExperimentManager {
	
	public List<Variable> listVariables() {
		Session session = HibernateUtil.getSession();
		VariableDao dao = new VariableDao(session);
		List<Variable> result = dao.list();
		session.flush();
		return result;
	}
	public Experiment submitExperiment(Experiment experiment) {
		experiment.setStatus(Experiment.Status.SUBMITED);
		Session session = HibernateUtil.getSession();
		ExperimentDao dao = new ExperimentDao(session);
		session.beginTransaction();
		dao.save(experiment);
		session.getTransaction().commit();
		session.close();
		return experiment;
	}

	public List<Experiment> listExperiments() {
		Session session = HibernateUtil.getSession();
		ExperimentDao dao = new ExperimentDao(session);
		List<Experiment> result = dao.list();
		session.flush();
		return result;
	}
	public void submitOccurrences(Set<Occurrence> occurences) {
		Session session = HibernateUtil.getSession();
		GenericDao<Occurrence, Long> dao = GenericDao.getInstance(session, Occurrence.class);
		session.beginTransaction();
		for (Occurrence occurrence: occurences){
			dao.save(occurrence);
		}
		session.getTransaction().commit();
		session.close();
	}

}
